﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using BCDataCenter;
using BCDataCenter.BOL;

public partial class Logon_Default : System.Web.UI.Page
{
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] != null)
        {
            Response.Redirect(Globals.ApplicationPath + "WebMaster/Default.aspx");
        }
        
    }
    static public string GetMd5Sum(string str)
    {
        // First we need to convert the string into bytes, which
        // means using a text encoder.

        Encoder enc = System.Text.Encoding.Unicode.GetEncoder();
        // Create a buffer large enough to hold the string

        byte[] unicodeText = new byte[str.Length * 2];

        enc.GetBytes(str.ToCharArray(), 0, str.Length, unicodeText, 0, true);

        // Now that we have a byte array we can ask the CSP to hash it

        MD5 md5 = new MD5CryptoServiceProvider();

        byte[] result = md5.ComputeHash(unicodeText);

        // Build the final string by converting each byte

        // into hex and appending it to a StringBuilder

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < result.Length; i++)
        {

            sb.Append(result[i].ToString("X2"));

        }
        // And return it
        return sb.ToString();
    }
    protected void btLogin_OnClick(object sender, EventArgs e)
    {
        string pass = GetMd5Sum(txtPassword.Text);

        BWUser obj = new BWUser();
        DataSet dsUserLogon = obj.dsGetUserLogon(ref error, txtUsername.Text, pass);

        if (dsUserLogon.Tables.Count != 0)
        {

            if (dsUserLogon.Tables[0].Rows.Count > 0)
            {
                Session["username"] = dsUserLogon.Tables[0].Rows[0]["UserName"].ToString();
                Session["Sitecode"] = dsUserLogon.Tables[0].Rows[0]["Sitecode"].ToString();
                Response.Redirect(Globals.ApplicationPath + "WebMaster/Default.aspx");
            }
            else
            {
                dsUserLogon = obj.dsGetUserViewReportLogon(ref error, txtUsername.Text, txtPassword.Text);
                if (dsUserLogon.Tables.Count != 0)
                {

                    if (dsUserLogon.Tables[0].Rows.Count > 0)
                    {
                        Session["username"] = "DefaultUser";
                        Session["Salesp_key_Logon"] = dsUserLogon.Tables[0].Rows[0]["salesp_key"].ToString();
                        Session["Sitecode"] = dsUserLogon.Tables[0].Rows[0]["BelongTo"].ToString();
                        Response.Redirect(Globals.ApplicationPath + "WebMaster/Default.aspx");
                    }
                    else
                        lblMsg.Text = "Tên đăng nhập hoặc mật khẩu không hợp lệ.";
                }
                else
                    lblMsg.Text = "Tên đăng nhập hoặc mật khẩu không hợp lệ.";
            }
                
        }
        else
        {
            dsUserLogon = obj.dsGetUserViewReportLogon(ref error, txtUsername.Text, txtPassword.Text);
            if (dsUserLogon.Tables.Count != 0)
            {

                if (dsUserLogon.Tables[0].Rows.Count > 0)
                {
                    Session["username"] = "DefaultUser";
                    Session["Salesp_key_Logon"] = dsUserLogon.Tables[0].Rows[0]["salesp_key"].ToString();
                    Session["Sitecode"] = dsUserLogon.Tables[0].Rows[0]["BelongTo"].ToString();
                    Response.Redirect(Globals.ApplicationPath + "WebMaster/Default.aspx");
                }
                else
                    lblMsg.Text = "Tên đăng nhập hoặc mật khẩu không hợp lệ.";
            }
            else
                lblMsg.Text = "Tên đăng nhập hoặc mật khẩu không hợp lệ.";
        }       
    }
}
